package com.linkage.educloud.ah.mvp.presenter;

import com.linkage.educloud.ah.Constants;
import com.linkage.educloud.ah.MyApplication;
import com.linkage.educloud.ah.bean.ClazzGroup;
import com.linkage.educloud.ah.config.AccountConfig;
import com.linkage.educloud.ah.http.Method;
import com.linkage.educloud.ah.http.RequestUtil;
import com.linkage.educloud.ah.mvp.IBasicView;
import com.linkage.educloud.ah.mvp.contract.IContactContract;
import com.linkage.educloud.ah.mvp.model.ContactModel;
import com.zhouyou.http.exception.ApiException;
import com.zhouyou.http.model.HttpParams;
import com.zhouyou.http.subsciber.BaseSubscriber;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * <desc>
 * Created by The Moss on 2018/11/8.
 */

public class ContactPresenter extends IContactContract.IPresenter {

    IContactContract.IModel iModel;
    public ContactPresenter(IBasicView iView) {
        super(iView);
        iModel = new ContactModel();
    }
    @SuppressWarnings("unchecked")
    @Override
    public void loadDatasFromLocal() {
        Observable<List<ClazzGroup>> observable = iModel.loadDatasFromLocal();
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(iView.bindLifecycle())
                .subscribe(new Consumer<List<ClazzGroup>>() {
                    @Override
                    public void accept(List<ClazzGroup> datas) throws Exception {
                        if (datas == null || datas.size() < 1) {
                            ((IContactContract.IView) iView).onLoadDatasFromLocal(false,null);
                        }
                        ((IContactContract.IView) iView).onLoadDatasFromLocal(true,datas);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        ((IContactContract.IView) iView).onLoadDatasFromLocal(false,null);
                    }
                });
    }
    @SuppressWarnings("unchecked")
    @Override
    public void loadDatasFromServer() {
        HttpParams params = RequestUtil.getGroupInfo();
        iModel.request(Method.POST,true,params).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(iView.bindLifecycle())
                .subscribe(new BaseSubscriber<String>() {
                    @Override
                    public void onError(ApiException e) {
                        ((IContactContract.IView) iView).onLoadDatasFromServer(false,null);
                    }

                    @Override
                    public void onNext(String s) {
                        try {
                            JSONObject json = new JSONObject(s);
                            if (json.getInt("result") == Constants.Result.result) {
                                List<ClazzGroup> groups = ClazzGroup.fromJsonObject(json);
                                MyApplication.getInstance().getResovler().insertGroups(AccountConfig.getLoginAccount().getLoginAccount(),groups);
//                                groups = MyApplication.getInstance().getResovler().getGroups(AccountConfig.getLoginAccount().getLoginAccount());
//                                ((IContactContract.IView) iView).onLoadDatasFromServer(true,groups);
                                loadDatasFromLocal();
                            }else{
                                ((IContactContract.IView) iView).onLoadDatasFromServer(false,null);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                            ((IContactContract.IView) iView).onLoadDatasFromServer(false,null);
                        }
                    }
                });
    }
}
